package com.yyt.module_main.act;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.yyt.libsrc.bean.QueryRateBean;
import com.yyt.libsrc.conf.Consts;
import com.yyt.libsrc.helper.MsgHelper;
import com.yyt.libsrc.net.JavaParamsBuilder;
import com.yyt.libsrc.net.NetUtil;
import com.yyt.libsrc.net.SuccessCallback;
import com.yyt.libsrc.router.RouteTable;
import com.yyt.libsrc.router.RouterHelper;
import com.yyt.module_main.R;
import com.yyt.module_main.databinding.ActivityMemberSettingBinding;
import com.yyt.module_main.vm.MemberSettingVM;

import top.lixb.libcommon.base.RefreshableActivity;
import top.lixb.libcommon.utils.StringUtils;

@Route(path = RouteTable.main_member_setting)
public class MemberSettingActivity extends RefreshableActivity<ActivityMemberSettingBinding> {

    private MemberSettingVM mVm;
    private String mSid;

    @Override
    protected void init() {
        mVm = new MemberSettingVM();
        mRefreshContentBinding.setVm(mVm);
        setTitle("成员信息");
        showToolbarDivider();
        Intent intent = getIntent();
        if (null != intent && null != intent.getExtras()) {
            String headicon = intent.getStringExtra("headicon");
            String name = intent.getStringExtra("name");
            mSid = intent.getStringExtra("sid");
            String rate = intent.getStringExtra("rate");
            int level = intent.getIntExtra("level", 0);
            boolean showsublist = intent.getBooleanExtra("showsublist", false);
            if (showsublist) {
                mRefreshContentBinding.tvChangeRate.setVisibility(View.INVISIBLE);
            } else {
                mRefreshContentBinding.tvChangeRate.setVisibility(View.VISIBLE);
            }
            mVm.iconUrl.set(headicon);
            mVm.name.set(name);
//            mVm.feeRate.set(TextUtils.isEmpty(rate)?"0.01":rate);
            mVm.role.set(level);
            mVm.sid.set(mSid);
            switch (level) {
                case 1:
                    mRefreshContentBinding.tvRole.setText("成员");
                    break;
                case 2:
                    mRefreshContentBinding.tvRole.setText("代理商");
                    break;
                case 3:
                    mRefreshContentBinding.tvRole.setText("合伙人");
                    break;
            }

        }
        mRefreshContentBinding.showSublist.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!TextUtils.isEmpty(mSid)) {
                    Bundle bundle = new Bundle();
                    bundle.putString("sid",mSid);
                    bundle.putInt("role",mVm.role.get());
                    bundle.putBoolean("showsublist",true);
                    RouterHelper.getInstance().startAct(RouteTable.main_member_list,bundle);
                } else {
                    MsgHelper.showToast("参数为空");
                }
            }
        });


        mTvRight.setText("回管理页");
        mTvRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RouterHelper.getInstance().startAct(RouteTable.main_member_manage);
            }
        });
    }

    @Override
    protected int initRefreshLayout() {
        return R.layout.activity_member_setting;
    }

    @Override
    protected void getData(boolean refresh) {
        NetUtil.sellerPost(new JavaParamsBuilder()
                .addCmd(Consts.CMD.queryRate)
                .addP("sid",mVm.sid.get())
                .build(), new SuccessCallback<QueryRateBean>() {
            @Override
            public void onSuccess(QueryRateBean queryRateBean) {
                double rate = queryRateBean.data.rate;
                mVm.feeRate.set(String.valueOf(rate));
            }

            @Override
            public void onComplete() {
                super.onComplete();
                finishGetData();
            }
        });
    }
}
